import java.util.*;

/**
 * @author LKQ
 * @date 2022/4/1 9:29
 * @description 思路：有序数组，从左下角开始遍历，遇到小于0 统计数量，然后向上；否则继续往右找第一个小于0的。循环
 */
public class Solution {
    public static void main(String[] args) {

    }
    public int countNegatives(int[][] grid) {
        int m = grid.length, n = grid[0].length;
        int i = m - 1, j = 0;
        int sum = 0;
        while (i >= 0 && j < n) {
            if (grid[i][j] < 0) {
                sum += n - j;
                i--;
            }else {
                j++;
            }
        }
        return sum;
    }
}
